package com.yalemang.mvp

import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner

abstract class BasePresenter<M:BaseModel,V:BaseView>(
     var mModel: M,//P层持有的Model
     var mView: V,//V层持有的View
){
     //用于让P层感知生命周期的
     protected lateinit var lifecycle: Lifecycle
     private var lifecycleObserver = object : DefaultLifecycleObserver {

          override fun onStop(owner: LifecycleOwner) {
               super.onStop(owner)
               onStop()
          }

          override fun onDestroy(owner: LifecycleOwner) {
               lifecycle.removeObserver(this)
               onDestroy()
          }
     }

     fun bindLifecycle(lifecycle: Lifecycle){
          this.lifecycle = lifecycle
          lifecycle.addObserver(lifecycleObserver)
     }

     //用于执行销毁的操作
     open fun onDestroy(){

     }

     //用于执行销毁的操作
     open fun onStop(){

     }
}